#include "stdio.h"
static inline int fork(){
	int num=170;
	int ret;
    asm volatile (
        "lw x17, %1\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num)
        : "memory"
      );
    return ret;
}


static inline int getpid(){
	int num=172;
	int ret;
    asm volatile (
        "lw x17, %1\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num)
        : "memory"
      );
    return ret;
}




static inline int wait(int * addr){
	int num=3;
	int ret;
    asm volatile (
        "lw x17, %1\n"
	"lw x10,%2\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num),"m"(addr)
        : "memory"
      );
    return ret;
}

int main(){
     int a=10;
     int ret=-1;
     int exit_status;
     printf("print proc app5_1\n");
     if((ret=fork()) == 0) {
        printf("print proc this is child process;my pid = %d\n",getpid());
        a=a-1;
	    printf("print proc a=%d\n",a);
    }else { 
        int wait_ret = -1;
    	wait_ret=wait(&exit_status);	
	    printf("print proc this is farther process;my pid = %d\n",getpid());
	    a=a-2;
        printf("print proc a=%d\n",a);
    }

    if((ret=fork()) == 0) {
        printf("print proc this is child process;my pid = %d\n",getpid());
	    a=a-3;
        printf("print proc a=%d\n",a);
    }else { 
        int wait_ret = -1;
    	wait_ret=wait(&exit_status);	
    	printf("print proc this is farther process;my pid = %d\n",getpid());
        a=a-4;
        printf("print proc a=%d\n",a);
    }

    return 0;
}
